﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PedesTrainProject.GUI
{
    public partial class Current_Statistic : Form
    {
        List<Data.Statistics> _statistics;
        Data.FileHandler fileHandler;

        public Current_Statistic()
        {
            InitializeComponent();
            statisticGrid.Rows.Add(DateTime.Now, DateTime.Now, "100", "10");
            statisticGrid.Rows.Add(DateTime.Now, DateTime.Now, "123", "2");
            statisticGrid.Invalidate();
        }

        public Current_Statistic(List<Data.Statistics> statistics)
        {
            InitializeComponent();
            this._statistics = statistics;
            foreach (Data.Statistics s in statistics)
            {
                statisticGrid.Rows.Add(s.StartTime, s.EndTime, s.CarTotal, s.JamTotal);
            }
            fileHandler = new Data.FileHandler();
        }

        private void loadSimulationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openStatisticsDialog = new OpenFileDialog();
            openStatisticsDialog.FileName = "";

            if (openStatisticsDialog.ShowDialog() == DialogResult.OK)
            {
                if (openStatisticsDialog.FileName != null && openStatisticsDialog.FileName != "")
                {
                    List<Data.Statistics> temp = fileHandler.loadStatistics(openStatisticsDialog.FileName);

                    if (temp != null)
                    {
                        _statistics = temp;
                        statisticGrid.Rows.Clear();
                        foreach (Data.Statistics s in _statistics)
                        {
                            statisticGrid.Rows.Add(s.StartTime, s.EndTime, s.CarTotal, s.JamTotal);
                        }
                        MessageBox.Show("Load Succeed");
                    }
                    else
                    {
                        MessageBox.Show("Load Failed");
                    }
                }
            }
        }

        private void exitSimulationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void saveSimulationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveStatisticsDialog = new SaveFileDialog();
            if (saveStatisticsDialog.ShowDialog() == DialogResult.OK)
            {
                if (saveStatisticsDialog.FileName != "" && saveStatisticsDialog.FileName != null)
                {
                    if (fileHandler.saveStatistics(saveStatisticsDialog.FileName, _statistics))
                    {
                        MessageBox.Show("Statistics have been saved");
                    }
                    else
                    {
                        MessageBox.Show("Failed to save");
                    }
                }
            }
        }

        private void saveAsPlainTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveStatisticsDialog = new SaveFileDialog();
            if (saveStatisticsDialog.ShowDialog() == DialogResult.OK)
            {
                if (saveStatisticsDialog.FileName != "" && saveStatisticsDialog.FileName != null)
                {
                    if (fileHandler.saveStatistics(saveStatisticsDialog.FileName, _statistics))
                    {
                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(saveStatisticsDialog.FileName))
                        {
                            foreach (Data.Statistics s in _statistics)
                            {
                                file.WriteLine("Start Time\t: " + s.StartTime);
                                file.WriteLine("End Time\t: " + s.EndTime);
                                file.WriteLine("Cars\t\t: " + s.CarTotal);
                                file.WriteLine("Traffic Jam\t: " + s.JamTotal);
                                file.WriteLine("");
                            }
                        }

                        MessageBox.Show("Statistics have been saved");
                    }
                    else
                    {
                        MessageBox.Show("Failed to save");
                    }
                }
            }
        }
    }
}
